package com.diamond.webservice.util;

import org.apache.commons.codec.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
        public static String sendHttpPost(String url, String body) throws Exception {
                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(url);
                httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
                httpPost.setHeader("Accept", "application/json");
                httpPost.setEntity(new StringEntity(body, Charsets.UTF_8));
                CloseableHttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                String responseContent = EntityUtils.toString(entity, "UTF-8");
                response.close();
                httpClient.close();
                return responseContent;
            }
}
